#include "fun.h"

u8 bdata com;
sbit BUZZER = com^6;
sbit RELAY = com^4;
u8 key_up = 1;
bit led_flag;
u8 ui_status,mode = 1,delay_t,update_flag,level_flag;
extern u8 s_count;

void buzzer(u8 cmd)
{
    BUZZER = cmd;
	P0 = com;
	P2 = 0xa0;
    _nop_();
    P2 = 0x00;
}

void relay(u8 cmd)
{
    RELAY = cmd;
    P0 = com;
    P2 = 0xa0;
    _nop_();
    P2 = 0x00;
}

void led(u8 ucode)
{
    P0 = ~ucode;
    P2 = 0x80;
    _nop_();
    P2 = 0x00;
}

void key_scan(void)
{
    if(key_up&&(P33==0||P32==0||P31==0||P30==0))
    {
        key_up = 0;
        if((!P33) && ui_status == 1 && (mode > 1))      //S4 
			mode--;
		else if((!P33) && ui_status == 2 && (delay_t > 4))  //S4
        {
			delay_t--;
            update_flag = 1;
        }
        else if((!P33) && ui_status == 0)
            level_flag = 1;
        else if((!P32) && ui_status == 1 && (mode < 4))       //S5
			mode++;
		else if((!P32) && ui_status == 2 && (delay_t < 12))   //S5
        {
			delay_t++;
            update_flag = 1;
        }
        else if(!P31)               //S6
        {
            if(++ui_status == 3)
            {
				ui_status = 0;
                s_count = 0;
                if(update_flag == 1)
                    update_flag = 2;
            }
        }
        else if(!P30)            //S7
		{
			led_flag = ~led_flag;
			mode = 1;
		}     
    }
    else if(P33&&P32&&P31&&P30)
        key_up = 1;
}

void smg_disp(u8 num,u8 position)
{
    P0 = 0xff;
    P2 = 0xe0;
    _nop_();
    P2 = 0x00;

    P0 = position;
    P2 = 0xc0;
    _nop_();
    P2 = 0x00;

    P0 = ~num;
    P2 = 0xe0;
    _nop_();
    P2 = 0x00;
}
